{% extends "basic.html" %}
{% block mainbody %}
{% load mytags %}
<form id="form1" class="form-horizontal" role="form" method="POST" action="/borrow/need/" style="padding-bottom:100px;">
  {% csrf_token %}
  
  <input type="hidden" name="id" value="{{ info.id }}"/>
  <h5 class="text-center">{{ info.borrow_name }}</h5>
  <div class="form-group">
    <label for="firstname" class="col-sm-2 control-label">资产</label>
    <div class="col-sm-10">
      <select name="assets_id" id="assets_id">
      {% for vo in assets_list %}
        <option value="{{ vo.id }}" {% ckSelectSelected vo.id info.assets_id %}>{{ vo.assets_name }}</option>
      {% endfor %}
      </select>
    </div>
  </div>
  <div class="form-group">
    <label for="firstname" class="col-sm-2 control-label"></label>
    <div class="col-sm-10">
      <div id="assets_detail"></div>
    </div>
  </div>
  <div class="form-group">
    <label for="firstname" class="col-sm-2 control-label">借款金额</label>
    <div class="col-sm-10">
      <div class="input-group">
        <span class="input-group-addon">¥</span>
        <input type="number" class="form-control" name="borrow_money" value="{{ info.borrow_money|toint }}">
        <span class="input-group-addon">元</span>
      </div>
    </div>
  </div>
  <div class="form-group">
    <label for="firstname" class="col-sm-2 control-label">还款方式</label>
    <div class="col-sm-10">
      <select name="repayment_type" id="repayment_type">
      {% for vo in arr_repayment_type %}
        <option value="{{ vo.k }}" {% ckSelectSelected vo.k info.repayment_type %}>{{ vo.v }}</option>
      {% endfor %}
      </select>
    </div>
  </div>
  <div class="form-group">
    <label for="firstname" class="col-sm-2 control-label">借款期限</label>
    <div class="col-sm-10">
      <div class="input-group">
        <input type="number" class="form-control" name="borrow_duration" value="{{ info.borrow_duration|returnBorrowDurationByRepaymentType:info.repayment_type|toint }}">
        <span class="input-group-addon" id="borrow_duration_unit">元</span>
      </div>
    </div>
  </div>
  <div class="form-group">
    <label for="firstname" class="col-sm-2 control-label">总年利率</label>
    <div class="col-sm-10">
      <div class="input-group">
        <input type="number" class="form-control" name="borrow_interest_rate" value="{{ info.borrow_interest_rate|tofloat }}">
        <span class="input-group-addon">%</span>
      </div>
    </div>
  </div>
  <div class="form-group">
    <label for="firstname" class="col-sm-2 control-label">最低投标金额</label>
    <div class="col-sm-10">
      <div class="input-group">
        <span class="input-group-addon">¥</span>
        <input type="number" class="form-control" name="borrow_min" value="{{ info.borrow_min|toint }}">
        <span class="input-group-addon">元</span>
      </div>
    </div>
  </div>
  <div class="form-group">
    <label for="firstname" class="col-sm-2 control-label">递增金额</label>
    <div class="col-sm-10">
      <div class="input-group">
        <span class="input-group-addon">¥</span>
        <input type="number" class="form-control" name="invest_step" value="{{ info.invest_step|toint }}">
        <span class="input-group-addon">元</span>
      </div>
    </div>
  </div>
  
  <div class="form-group">
    <label for="firstname" class="col-sm-2 control-label">类型</label>
    <div class="col-sm-10">
      {% for vo in whether_or_not %}
        <label class="checkbox-inline"><input type="radio" name="is_use_coupons" value="{{ vo.k }}" {% if vo.k == info.is_use_coupons|stringToInt %}checked{% endif %}>{{ vo.v }}</label>
      {% endfor %}
    </div>
  </div>
  <div class="form-group">
    <label for="firstname" class="col-sm-2 control-label">状态</label>
    <div class="col-sm-10">
      <select name="borrow_status">
      {% for vo in arr_borrow_status %}
        <option value="{{ vo.k }}" {% ckSelectSelected vo.k info.borrow_status %} {% ckIsNotDisabled vo.k "0,-20,20,21,22,0" %} > {{ vo.v }} </option>
      {% endfor %}
      </select>
    </div>
  </div>
  
  <div id="btn_submit_fixed_bootom" class="form-group navbar-fixed-bottom" style="margin:5px;">
    <button type="submit" class="btn btn-primary btn-lg btn-block">提交</button>
  </div>
  <script type="text/javascript">document.write( '<style>div#btn_submit_fixed_bootom{visibility:hidden}@media(min-height:' + ($( window ).height() - 10) + 'px){div#btn_submit_fixed_bootom{visibility:visible}}</style>' );</script>
</form>
<script type="text/javascript">
$(document).ready(function(){
  var assets_id="{{ info.assets_id }}";
  if(assets_id == "0"){
    assets_id=$("#assets_id").val();
  }
  getinvest(parseInt(assets_id));
  
  var repayment_type="{{ info.repayment_type }}";
  if(repayment_type == "0"){
    repayment_type=$("#repayment_type").val();
  }
  set_borrow_duration_unit(repayment_type);
});

$("#assets_id").change(function(){
  var assets_id=parseInt($(this).find("option:selected").val());
  if(assets_id==0){
    $("#assets_detail").html("");
    return;
  }
  getinvest(assets_id);
});
function getinvest(assets_id){
  var url="/assets/getinvest/"+"?&id="+assets_id;
  var data="csrfmiddlewaretoken="+$("input[name='csrfmiddlewaretoken']").val();
  
  var tips=art.dialog.tips("正在提交……");
  $.ajax({
    async:false,
    type:'POST',
    url:url,
    data:data,
    success:function(rsObj){
      if(rsObj.response_code=="1"){
        var html="";
        html+='资产总额：'+rsObj.assets.capital+'元，起息时间：'+rsObj.assets.interest_dt_txt+'，到期时间：'+rsObj.assets.exp_dt_txt+'，期限：'+rsObj.assets.dt+'天，资产剩余额度：'+rsObj.assets.surplus_money+'元'+'<br/>';
        $("#assets_detail").html(html);
      }else{
        art.dialog.alert(rsObj.response_message);
      }
    },
    cache:false
  });
  return false;
  
}

$("#repayment_type").change(function(){
	var v=$(this).val();
  set_borrow_duration_unit(v);
});
function set_borrow_duration_unit(v){
  if(v=="1"){
		$("#borrow_duration_unit").text("天");
	}else{
		$("#borrow_duration_unit").text("个月");
	}
}
</script>

<script type="text/javascript">
$(document).ready(function(){
  $("#form1").submit(function(){
    //return true;
    var data=$(this).serialize();
    var url=$(this).attr("action");
    var tips=art.dialog.tips("正在提交……");
    
    $.ajax({
      async:false,
      type:'POST',
      url:url,
      data:data,
      success:function(rsObj){
        if(rsObj.response_code=="1"){
          art.dialog({
            content:rsObj.response_message,
            cancelVal:'继续编辑',
            cancel:function(){
              tips.close();
            },
            okVal:'返回',
            ok:function(){
              javascript:window.history.go(-1);
            }
          });
        }else{
          art.dialog.alert(rsObj.response_message);
        }
      },
      cache:false
    });
    return false;
  });
});
</script>
{% endblock %}